#ifndef __DICT__H_H_
#define __DICT__H_H_

#include "stdint.h"
#include "mytype.h"
#include "mem_buffer.h"

#ifdef __cplusplus
extern "C"
{
#endif

#define DICT_LEN(x) (sizeof(x) / sizeof(x[0]))

	typedef struct
	{
		uint8_t src;
		uint8_t des;
	} dict_u8_type;

	typedef struct
	{
		int16_t len_max;
		int16_t len_now;
		dict_u8_type *list;
	} dict_u8_list_type;

	typedef struct
	{
		dict_u8_list_type flist;
		int len_limit;
	} dict_u8_mlist_type;

	int16_t dict_u8_search(const dict_u8_type *dict, int16_t Len, uint8_t src, bisect_mode_type mode); 
	int16_t dict_u8_des_traverse(const dict_u8_type* dict, int16_t Len, uint8_t des); 
	int16_t dict_u8_des_search(const dict_u8_type* dict, int16_t Len, uint8_t des, bisect_mode_type mode); 
	void dict_u8_sort(dict_u8_type* dict, int16_t Len); 
	void dict_u8_select_sort(dict_u8_type* dict, int16_t Len); 

	int16_t dict_u8_from_arr(dict_u8_type* dict, const uint8_t* arr, int16_t Len, u8_rang_type src_r, u8_rang_type des_r); 
	int16_t dict_u8_init(dict_u8_list_type *obj, int16_t len_max, dict_u8_type *list); 
	int16_t dict_u8_insert(dict_u8_list_type *obj, int32_t site, const dict_u8_type *member); 
	int16_t dict_u8_add(dict_u8_list_type *obj, const dict_u8_type *member); 
	int16_t dict_u8_del(dict_u8_list_type *obj, int16_t site); 

	int16_t dict_u8_mlist_init(dict_u8_mlist_type* obj, int32_t len_limit, int32_t len_init);
	int16_t dict_u8_mlist_insert(dict_u8_mlist_type* obj, int32_t site, const dict_u8_type* member);
	int16_t dict_u8_mlist_add(dict_u8_mlist_type* obj, const dict_u8_type* member);
	void dict_u8_mlist_free(dict_u8_mlist_type* obj);

	typedef struct
	{
		int8_t src;
		int8_t des;
	} dict_i8_type;

	typedef struct
	{
		int16_t len_max;
		int16_t len_now;
		dict_i8_type *list;
	} dict_i8_list_type;

	typedef struct
	{
		dict_i8_list_type flist;
		int len_limit;
	} dict_i8_mlist_type;

	int16_t dict_i8_search(const dict_i8_type *dict, int16_t Len, int8_t src, bisect_mode_type mode);
	int16_t dict_i8_des_traverse(const dict_i8_type* dict, int16_t Len, int8_t des);
	int16_t dict_i8_des_search(const dict_i8_type* dict, int16_t Len, int8_t des, bisect_mode_type mode);
	void dict_i8_sort(dict_i8_type* dict, int16_t Len);
	void dict_i8_select_sort(dict_i8_type* dict, int16_t Len);
	int16_t dict_i8_from_arr(dict_i8_type* dict, const int8_t* arr, int16_t Len, i8_rang_type src_r, i8_rang_type des_r);
	int16_t dict_i8_init(dict_i8_list_type *obj, int16_t len_max, dict_i8_type *list);
	int16_t dict_i8_insert(dict_i8_list_type *obj, int32_t site, const dict_i8_type *member);
	int16_t dict_i8_add(dict_i8_list_type *obj, const dict_i8_type *member);
	int16_t dict_i8_del(dict_i8_list_type *obj, int16_t site);

	int16_t dict_i8_mlist_init(dict_i8_mlist_type* obj, int32_t len_limit, int32_t len_init);
	int16_t dict_i8_mlist_insert(dict_i8_mlist_type* obj, int32_t site, const dict_i8_type* member);
	int16_t dict_i8_mlist_add(dict_i8_mlist_type* obj, const dict_i8_type* member);
	void dict_i8_mlist_free(dict_i8_mlist_type* obj);

	typedef struct
	{
		uint16_t src;
		uint16_t des;
	} dict_u16_type;

	typedef struct
	{
		int32_t len_max;
		int32_t len_now;
		dict_u16_type *list;
	} dict_u16_list_type;

	typedef struct
	{
		dict_u16_list_type flist;
		int len_limit;
	} dict_u16_mlist_type;

	int16_t dict_u16_search(const dict_u16_type *dict, int16_t Len, uint16_t src, bisect_mode_type mode);
	int16_t dict_u16_des_traverse(const dict_u16_type* dict, int16_t Len, uint16_t des);
	int16_t dict_u16_des_search(const dict_u16_type* dict, int16_t Len, uint16_t des, bisect_mode_type mode);
	void dict_u16_sort(dict_u16_type* dict, int16_t Len);
	void dict_u16_select_sort(dict_u16_type* dict, int16_t Len);
	int16_t dict_u16_from_arr(dict_u16_type* dict, const uint16_t* arr, int16_t Len, u16_rang_type src_r, u16_rang_type des_r);
	int16_t dict_u16_init(dict_u16_list_type *obj, int16_t len_max, dict_u16_type *list);
	int16_t dict_u16_insert(dict_u16_list_type *obj, int32_t site, const dict_u16_type *member);
	int16_t dict_u16_add(dict_u16_list_type *obj, const dict_u16_type *member);
	int16_t dict_u16_del(dict_u16_list_type *obj, int16_t site);

	int16_t dict_u16_mlist_init(dict_u16_mlist_type* obj, int32_t len_limit, int32_t len_init);
	int16_t dict_u16_mlist_insert(dict_u16_mlist_type* obj, int32_t site, const dict_u16_type* member);
	int16_t dict_u16_mlist_add(dict_u16_mlist_type* obj, const dict_u16_type* member);
	void dict_u16_mlist_free(dict_u16_mlist_type* obj);

	typedef struct
	{
		int16_t src;
		int16_t des;
	} dict_i16_type;

	typedef struct
	{
		int32_t len_max;
		int32_t len_now;
		dict_i16_type *list;
	} dict_i16_list_type;

	typedef struct
	{
		dict_i16_list_type flist;
		int len_limit;
	} dict_i16_mlist_type;

	int16_t dict_i16_search(const dict_i16_type *dict, int16_t Len, int16_t src, bisect_mode_type mode);
	int16_t dict_i16_des_traverse(const dict_i16_type* dict, int16_t Len, int16_t des);
	int16_t dict_i16_des_search(const dict_i16_type* dict, int16_t Len, int16_t des, bisect_mode_type mode);
	void dict_i16_sort(dict_i16_type* dict, int16_t Len);
	void dict_i16_select_sort(dict_i16_type* dict, int16_t Len);
	int16_t dict_i16_from_arr(dict_i16_type* dict, const int16_t* arr, int16_t Len, i16_rang_type src_r, i16_rang_type des_r);
	int16_t dict_i16_init(dict_i16_list_type *obj, int16_t len_max, dict_i16_type *list);
	int16_t dict_i16_insert(dict_i16_list_type *obj, int32_t site, const dict_i16_type *member);
	int16_t dict_i16_add(dict_i16_list_type *obj, const dict_i16_type *member);
	int16_t dict_i16_del(dict_i16_list_type *obj, int16_t site);

	int16_t dict_i16_mlist_init(dict_i16_mlist_type* obj, int32_t len_limit, int32_t len_init);
	int16_t dict_i16_mlist_insert(dict_i16_mlist_type* obj, int32_t site, const dict_i16_type* member);
	int16_t dict_i16_mlist_add(dict_i16_mlist_type* obj, const dict_i16_type* member);
	void dict_i16_mlist_free(dict_i16_mlist_type* obj);

	typedef struct
	{
		uint32_t src;
		uint32_t des;
	} dict_u32_type;

	typedef struct
	{
		int32_t len_max;
		int32_t len_now;
		dict_u32_type* list;
	} dict_u32_list_type;

	typedef struct
	{
		dict_u32_list_type flist;
		int len_limit;
	} dict_u32_mlist_type;

	int32_t dict_u32_search(const dict_u32_type* dict, int32_t Len, uint32_t src, bisect_mode_type mode);
	int32_t dict_u32_des_traverse(const dict_u32_type* dict, int32_t Len, uint32_t des);
	int32_t dict_u32_des_search(const dict_u32_type* dict, int32_t Len, uint32_t des, bisect_mode_type mode);
	void dict_u32_sort(dict_u32_type* dict, int32_t Len);
	void dict_u32_select_sort(dict_u32_type* dict, int32_t Len);
	int32_t dict_u32_from_arr(dict_u32_type* dict, const int32_t* arr, int32_t Len, u32_rang_type src_r, u32_rang_type des_r);
	int32_t dict_u32_init(dict_u32_list_type* obj, int32_t len_max, dict_u32_type* list);
	int32_t dict_u32_insert(dict_u32_list_type* obj, int32_t site, const dict_u32_type* member);
	int32_t dict_u32_add(dict_u32_list_type* obj, const dict_u32_type* member);
	int32_t dict_u32_del(dict_u32_list_type* obj, int32_t site);

	int32_t dict_u32_mlist_init(dict_u32_mlist_type* obj, int32_t len_limit, int32_t len_init);
	int32_t dict_u32_mlist_insert(dict_u32_mlist_type* obj, int32_t site, const dict_u32_type* member);
	int32_t dict_u32_mlist_add(dict_u32_mlist_type* obj, const dict_u32_type* member);
	void dict_u32_mlist_free(dict_u32_mlist_type* obj);

	typedef struct
	{
		int32_t src;
		int32_t des;
	} dict_i32_type;

	typedef struct
	{
		int32_t len_max;
		int32_t len_now;
		dict_i32_type *list;
	} dict_i32_list_type;

	typedef struct
	{
		dict_i32_list_type flist;
		int len_limit;
	} dict_i32_mlist_type;

	int32_t dict_i32_search(const dict_i32_type *dict, int32_t Len, int32_t src, bisect_mode_type mode);
	int32_t dict_i32_des_traverse(const dict_i32_type* dict, int32_t Len, int32_t des);
	int32_t dict_i32_des_search(const dict_i32_type* dict, int32_t Len, int32_t des, bisect_mode_type mode);
	void dict_i32_sort(dict_i32_type* dict, int32_t Len);
	void dict_i32_select_sort(dict_i32_type* dict, int32_t Len);
	int32_t dict_i32_from_arr(dict_i32_type* dict, const int32_t* arr, int32_t Len, i32_rang_type src_r, i32_rang_type des_r);
	int32_t dict_i32_init(dict_i32_list_type *obj, int32_t len_max, dict_i32_type *list);
	int32_t dict_i32_insert(dict_i32_list_type *obj, int32_t site, const dict_i32_type *member);
	int32_t dict_i32_add(dict_i32_list_type *obj, const dict_i32_type *member);
	int32_t dict_i32_del(dict_i32_list_type *obj, int32_t site);

	int32_t dict_i32_mlist_init(dict_i32_mlist_type* obj, int32_t len_limit, int32_t len_init);
	int32_t dict_i32_mlist_insert(dict_i32_mlist_type* obj, int32_t site, const dict_i32_type* member);
	int32_t dict_i32_mlist_add(dict_i32_mlist_type* obj, const dict_i32_type* member);
	void dict_i32_mlist_free(dict_i32_mlist_type* obj);

	typedef struct
	{
		float src;
		float des;
	} dict_f32_type;

	typedef struct
	{
		int32_t len_max;
		int32_t len_now;
		dict_f32_type* list;
	} dict_f32_list_type;

	typedef struct
	{
		dict_f32_list_type flist;
		int len_limit;
	} dict_f32_mlist_type;

	int32_t dict_f32_search(const dict_f32_type* dict, int32_t Len, float src, bisect_mode_type mode);
	int32_t dict_f32_des_traverse(const dict_f32_type* dict, int32_t Len, float des);
	int32_t dict_f32_des_search(const dict_f32_type* dict, int32_t Len, float des, bisect_mode_type mode);
	void dict_f32_sort(dict_f32_type* dict, int32_t Len);
	void dict_f32_select_sort(dict_f32_type* dict, int32_t Len);
	int32_t dict_f32_from_arr(dict_f32_type* dict, const float* arr, int32_t Len, f32_rang_type src_r, f32_rang_type des_r);
	int32_t dict_f32_init(dict_f32_list_type* obj, int32_t len_max, dict_f32_type* list);
	int32_t dict_f32_insert(dict_f32_list_type* obj, int32_t site, const dict_f32_type* member);
	int32_t dict_f32_add(dict_f32_list_type* obj, const dict_f32_type* member);
	int32_t dict_f32_del(dict_f32_list_type* obj, int32_t site);

	int32_t dict_f32_mlist_init(dict_f32_mlist_type* obj, int32_t len_limit, int32_t len_init);
	int32_t dict_f32_mlist_insert(dict_f32_mlist_type* obj, int32_t site, const dict_f32_type* member);
	int32_t dict_f32_mlist_add(dict_f32_mlist_type* obj, const dict_f32_type* member);
	void dict_f32_mlist_free(dict_f32_mlist_type* obj);

	typedef struct
	{
		int32_t src;
		const char *des;
	} dict_i32_str_type;

	typedef struct
	{
		int32_t len_max;
		int32_t len_now;
		dict_i32_str_type *list;
	} dict_i32_str_list_type;

	int16_t dict_i32_str_search(const dict_i32_str_type *dict, int16_t Len, int32_t src, bisect_mode_type mode);
	void dict_i32_str_sort(dict_i32_str_type* dict, int32_t Len);
	void dict_i32_str_select_sort(dict_i32_str_type* dict, int32_t Len);
	int16_t dict_i32_str_init(dict_i32_str_list_type *obj, int16_t len_max, dict_i32_str_type *list);
	int16_t dict_i32_str_insert(dict_i32_str_list_type *obj, int32_t site, const dict_i32_str_type *member);
	int16_t dict_i32_str_add(dict_i32_str_list_type *obj, const dict_i32_str_type *member);
	int16_t dict_i32_str_del(dict_i32_str_list_type *obj, int16_t site);

	typedef struct
	{
		const char *src;
		const char *des;
	} dict_str_type;

	typedef struct
	{
		int32_t len_max;
		int32_t len_now;
		dict_str_type *list;
		mem_fbuf_type *mem;
	} dict_str_list_type;

	void dict_str_sort(dict_str_type *dict, int32_t len);
	
	int32_t dict_str_init(dict_str_list_type *obj, int32_t len_max, dict_str_type *list, mem_fbuf_type *mem);
	int32_t dict_str_search(dict_str_list_type *obj, const char *src);
	int32_t dict_str_add(dict_str_list_type *obj, const dict_str_type *str);
	int32_t dict_str_insert(dict_str_list_type *obj, int32_t site, const dict_str_type *str);
	void dict_str_del(dict_str_list_type *obj, int32_t site);

	typedef struct
	{
		dict_str_list_type slist;
		int32_t mem_len_limit; 
	} dict_str_mlist_type;
	int32_t dict_str_mlist_init(dict_str_mlist_type *obj, int32_t mem_len_limit, int32_t len_init, mem_fbuf_type *mem);
	int32_t dict_str_mlist_search(dict_str_mlist_type* obj, const char* src);
	int32_t dict_str_mlist_insert(dict_str_mlist_type *mlist, int32_t site, const dict_str_type *obj);
	int32_t dict_str_mlist_add(dict_str_mlist_type *mlist, const dict_str_type *obj);
	int32_t dict_str_mlist_free(dict_str_mlist_type* mlist);

	typedef struct
	{
		size_t src;
		size_t des;
	} dict_mbase_str_type;

	typedef struct
	{
		int32_t len_max;
		int32_t len_now;
		dict_mbase_str_type* list;
		mem_mbuf_type *mem;
	} dict_mbase_str_list_type;

	int32_t dict_mbase_str_init(dict_mbase_str_list_type* obj, int32_t len_max, dict_mbase_str_type* list, mem_mbuf_type *mem);
	int32_t dict_mbase_str_search(dict_mbase_str_list_type* obj, const char* src);
	int32_t dict_mbase_str_add(dict_mbase_str_list_type* obj, const dict_str_type* str);
	int32_t dict_mbase_str_insert(dict_mbase_str_list_type* obj, int32_t site, const dict_str_type* str);
	void dict_mbase_str_del(dict_mbase_str_list_type* obj, int32_t site);

#define dict_mbase_str_convert(dict, site, res) \
	do {\
	res.src = (char*)dict.mem->mem + dict.list[site].src;\
	res.des = (char*)dict.mem->mem + dict.list[site].des;\
	}while(0)

	typedef struct
	{
		dict_mbase_str_list_type slist;
		int32_t mem_len_limit; 
	} dict_mbase_str_mlist_type;
	int32_t dict_mbase_str_mlist_init(dict_mbase_str_mlist_type* mlist, int32_t mem_len_limit, int32_t len_init, mem_mbuf_type *mem);
	int32_t dict_mbase_str_mlist_search(dict_mbase_str_mlist_type* mlist, const char* src);
	int32_t dict_mbase_str_mlist_insert(dict_mbase_str_mlist_type* mlist, int32_t site, const dict_str_type* obj);
	int32_t dict_mbase_str_mlist_add(dict_mbase_str_mlist_type* mlist, const dict_str_type* obj);
	int32_t dict_mbase_str_mlist_free(dict_mbase_str_mlist_type* mlist);

#ifdef __cplusplus
}
#endif

#endif 
